﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using GameStateManagement;

namespace DrWileyO_REDACTED_Adventure
{
    class SoundManager
    {
        private static SoundManager _instance;

        ScreenManager sManager;
        ContentManager content;

        Random rand;

        private SoundEffectInstance[] sounds;

        public enum Sound{
            intro = 0,
            death = -1,
            hurt = 3,
            jump = 4,
            pickup = 5,
            shoot = 6,
            shrink = 7,
            taunt = -2,
            menuBlip = 10
        }

        public SoundManager(ScreenManager sManager)
        {
            this.sManager = sManager;
            content = new ContentManager(sManager.Game.Services, "Content");
            sounds = new SoundEffectInstance[11];
            loadSounds();
            rand = new Random();
        }

        private SoundManager()
        {
            if (content == null)
                content = new ContentManager(sManager.Game.Services, "Content");
            sounds = new SoundEffectInstance[11];
            loadSounds();
            rand = new Random();
        }

        public static SoundManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new SoundManager();

                return _instance;
            }

            set
            {
                _instance = value;
            }
        }

        public void playSound(Sound toPlay)
        {
            if (toPlay == Sound.death)
            {
                if (rand.Next(2) == 0)
                    sounds[1].Play();
                else
                    sounds[2].Play();
            }
            else if (toPlay == Sound.taunt)
            {
                if (rand.Next(2) == 0)
                    sounds[8].Play();
                else
                    sounds[9].Play();
            }
            else
            {
                sounds[(int)toPlay].Play();
            }
        }

        public void playSound(Sound toPlay, float pitch)
        {
            SoundEffectInstance tempSound;
            if (toPlay == Sound.death)
            {
                if (rand.Next(2) == 0)
                    tempSound = sounds[1];
                else
                    tempSound = sounds[2];
            }
            else if (toPlay == Sound.taunt)
            {
                if (rand.Next(2) == 0)
                    tempSound = sounds[8];
                else
                    tempSound = sounds[9];
            }
            else
            {
                tempSound = sounds[(int)toPlay];
            }

            tempSound.Pitch = pitch;
            tempSound.Play();
        }

        private void loadSounds()
        {
            SoundEffect intro = content.Load<SoundEffect>("intro");
            SoundEffect death1 = content.Load<SoundEffect>("ScienceBetrayed");
            SoundEffect death2 = content.Load<SoundEffect>("WhatHasScienceDone");
            SoundEffect hurt = content.Load<SoundEffect>("hurt");
            SoundEffect jump = content.Load<SoundEffect>("jump");
            SoundEffect pickup = content.Load<SoundEffect>("pickup");
            SoundEffect shoot = content.Load<SoundEffect>("shoot");
            SoundEffect shrink = content.Load<SoundEffect>("shrink");
            SoundEffect taunt1 = content.Load<SoundEffect>("HAHA");
            SoundEffect taunt2 = content.Load<SoundEffect>("SCIENCE");
            SoundEffect menublip = content.Load<SoundEffect>("menublip");

            sounds[0] = intro.CreateInstance();
            sounds[1] = death1.CreateInstance();
            sounds[2] = death2.CreateInstance();
            sounds[3] = hurt.CreateInstance();
            sounds[4] = jump.CreateInstance();
            sounds[5] = pickup.CreateInstance();
            sounds[6] = shoot.CreateInstance();
            sounds[7] = shrink.CreateInstance();
            sounds[8] = taunt1.CreateInstance();
            sounds[9] = taunt2.CreateInstance();
            sounds[10] = menublip.CreateInstance();
        }

        public void StopAll()
        {
            for (int index = 0; index < sounds.Length; ++index)
            {
                sounds[index].Stop();
            }
        }

        public void StopSound(Sound toStop)
        {
            sounds[(int)toStop].Stop();
        }
    }
}
